from django.db import models

# Create your models here.
class LinesDetail(models.Model):
    news_name = models.CharField(max_length=255)
    num_places = models.IntegerField()
    source_city = models.CharField(max_length=25)
    detail = models.TextField()
    heat_rate = models.IntegerField(null=True)
    created = models.DateTimeField(auto_now_add=True)
    is_gd = models.BooleanField(default=False)

    class Meta:
        ordering = ('created',)


class VoteGroup(models.Model):
    title = models.CharField(max_length=255)
    is_activte = models.BooleanField(default=True)
    created = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ('created',)


class VoteItem(models.Model):
    item_id = models.CharField(max_length=128)
    title = models.CharField(max_length=255)
    vote_count = models.IntegerField()
    url = models.URLField(null=True)
    pic_url = models.URLField(null=True)
    point_x = models.CharField(max_length=25)
    point_y = models.CharField(max_length=25)
    created = models.DateTimeField(auto_now_add=True)
    is_activte = models.BooleanField(default=True)

    vote = models.ForeignKey(VoteGroup, on_delete=models.CASCADE,)

    class Meta:
        ordering = ('created',)

    def to_json(self):
        return {
            'title': self.title,
            'count': self.vote_count,
            'url': self.url or "",
            'pic_url': self.pic_url or "",
            "geometry": {
                "type": "Point",
                "coordinates": [self.point_x, self.point_y]
            },
            "item_id": self.item_id,
            "activate": self.is_activte,
            "vote_id": self.vote.id,
            "name": "{id}:{count}".format(id=self.item_id, count=self.vote_count)
        }